<template>
  <keep-alive>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="是否有效" prop="isActive">
          <el-select v-model="queryParams.isActive" placeholder="请选择是否有效" clearable size="small">
            <el-option
              v-for="dict in isActiveOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="活动名称" prop="qyyxAcName">
          <el-input
            v-model="queryParams.qyyxAcName"
            placeholder="请输入活动名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="活动开始时间" prop="qyyxAcStart">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="queryParams.qyyxAcStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="qyyxAcEnd">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="queryParams.qyyxAcEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择活动结束时间">
          </el-date-picker>
        </el-form-item> -->
        
        <el-form-item label="一级销区名称" prop="qyyxAcXq1Name">
          <el-input
            v-model="queryParams.qyyxAcXq1Name"
            placeholder="请输入一级销区名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="二级销区名称" prop="qyyxAcXq2Name">
          <el-input
            v-model="queryParams.qyyxAcXq2Name"
            placeholder="请输入二级销区名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="品牌名称" prop="qyyxAcPpName">
          <el-input
            v-model="queryParams.qyyxAcPpName"
            placeholder="请输入品牌名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <!-- v-hasPermi="['qyyx:activity:add']" -->
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" border stripe :data="activityList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="活动名称" align="center" prop="qyyxAcName" />
        <!-- <el-table-column label="活动头图" align="center" prop="qyyxAcImg" />
        <el-table-column label="活动分享图" align="center" prop="qyyxAcShareImg" /> -->
        <!-- <el-table-column label="活动内容" align="center" prop="qyyxAcContent" /> -->
        <el-table-column label="活动开始时间" align="center" prop="qyyxAcStart" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.qyyxAcStart, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="活动结束时间" align="center" prop="qyyxAcEnd" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.qyyxAcEnd, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="一级销区" align="center" prop="qyyxAcXq1Name" />
        <el-table-column label="二级销区" align="center" prop="qyyxAcXq2Name" />
        <el-table-column label="品牌" align="center" prop="qyyxAcPpName" />
        <el-table-column label="获客奖励" align="center" prop="qyyxAcHk" />
        <el-table-column label="获客奖励类型" align="center" prop="qyyxAcHkType" :formatter="qyyxAcHkTypeFormat"/>
        <el-table-column label="成交奖励" align="center" prop="qyyxAcCj" />
        <el-table-column label="成交奖励类型" align="center" prop="qyyxAcCjType" :formatter="qyyxAcCjTypeFormat"/>
        <el-table-column label="活动客服" align="center" prop="qyyxAcKf" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <!-- <el-table-column label="预留1" align="center" prop="lzjbYl1" />
        <el-table-column label="预留2" align="center" prop="lzjbYl2" />
        <el-table-column label="预留3" align="center" prop="lzjbYl3" />
        <el-table-column label="预留4" align="center" prop="lzjbYl4" />
        <el-table-column label="预留5" align="center" prop="lzjbYl5" />
        <el-table-column label="预留6" align="center" prop="lzjbYl6" /> -->
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['qyyx:activity:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              @click="toEwm(scope.row)"
            >管理二维码</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['qyyx:activity:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改全员营销活动信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="是否有效">
            <el-select v-model="form.isActive" placeholder="请选择是否有效">
              <el-option
                v-for="dict in isActiveOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="租户id" prop="lesseeId">
            <el-input v-model="form.lesseeId" placeholder="请输入租户id" />
          </el-form-item>
          <el-form-item label="活动名称" prop="qyyxAcName">
            <el-input v-model="form.qyyxAcName" placeholder="请输入活动名称" />
          </el-form-item>
          <el-form-item label="活动头图" prop="qyyxAcImg">
            <el-input v-model="form.qyyxAcImg" placeholder="请输入活动头图" />
          </el-form-item>
          <el-form-item label="活动分享图" prop="qyyxAcShareImg">
            <el-input v-model="form.qyyxAcShareImg" placeholder="请输入活动分享图" />
          </el-form-item>
          <el-form-item label="活动内容" prop="qyyxAcContent">
            <el-input v-model="form.qyyxAcContent" placeholder="请输入活动内容" />
          </el-form-item>
          <el-form-item label="活动开始时间" prop="qyyxAcStart">
            <el-date-picker clearable size="small" style="width: 200px"
              v-model="form.qyyxAcStart"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择活动开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="活动结束时间" prop="qyyxAcEnd">
            <el-date-picker clearable size="small" style="width: 200px"
              v-model="form.qyyxAcEnd"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择活动结束时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="一级销区" prop="qyyxAcXq1">
            <el-input v-model="form.qyyxAcXq1" placeholder="请输入一级销区" />
          </el-form-item>
          <el-form-item label="一级销区名称" prop="qyyxAcXq1Name">
            <el-input v-model="form.qyyxAcXq1Name" placeholder="请输入一级销区名称" />
          </el-form-item>
          <el-form-item label="二级销区" prop="qyyxAcXq2">
            <el-input v-model="form.qyyxAcXq2" placeholder="请输入二级销区" />
          </el-form-item>
          <el-form-item label="二级销区名称" prop="qyyxAcXq2Name">
            <el-input v-model="form.qyyxAcXq2Name" placeholder="请输入二级销区名称" />
          </el-form-item>
          <el-form-item label="品牌" prop="qyyxAcPp">
            <el-input v-model="form.qyyxAcPp" placeholder="请输入品牌" />
          </el-form-item>
          <el-form-item label="品牌名称" prop="qyyxAcPpName">
            <el-input v-model="form.qyyxAcPpName" placeholder="请输入品牌名称" />
          </el-form-item>
          <el-form-item label="获客奖励" prop="qyyxAcHk">
            <el-input v-model="form.qyyxAcHk" placeholder="请输入获客奖励" />
          </el-form-item>
          <el-form-item label="获客奖励类型">
            <el-select v-model="form.qyyxAcHkType" placeholder="请选择获客奖励类型">
              <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
          <el-form-item label="成交奖励" prop="qyyxAcCj">
            <el-input v-model="form.qyyxAcCj" placeholder="请输入成交奖励" />
          </el-form-item>
          <el-form-item label="成交奖励类型">
            <el-select v-model="form.qyyxAcCjType" placeholder="请选择成交奖励类型">
              <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
          <el-form-item label="活动客服" prop="qyyxAcKf">
            <el-input v-model="form.qyyxAcKf" placeholder="请输入活动客服" />
          </el-form-item>
          <el-form-item label="预留1" prop="lzjbYl1">
            <el-input v-model="form.lzjbYl1" placeholder="请输入预留1" />
          </el-form-item>
          <el-form-item label="预留2" prop="lzjbYl2">
            <el-input v-model="form.lzjbYl2" placeholder="请输入预留2" />
          </el-form-item>
          <el-form-item label="预留3" prop="lzjbYl3">
            <el-input v-model="form.lzjbYl3" placeholder="请输入预留3" />
          </el-form-item>
          <el-form-item label="预留4" prop="lzjbYl4">
            <el-input v-model="form.lzjbYl4" placeholder="请输入预留4" />
          </el-form-item>
          <el-form-item label="预留5" prop="lzjbYl5">
            <el-input v-model="form.lzjbYl5" placeholder="请输入预留5" />
          </el-form-item>
          <el-form-item label="预留6" prop="lzjbYl6">
            <el-input v-model="form.lzjbYl6" placeholder="请输入预留6" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </keep-alive>

</template>

<script>
import { setToken, getToken, setLesseeId } from '@/utils/auth'
import { listActivity, getActivity, delActivity, addActivity, updateActivity, exportActivity } from "@/api/qyyx/activity";

export default {
  name: "Activity",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 全员营销活动信息表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      isActiveOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isActive: undefined,
        qyyxAcName: undefined,
        qyyxAcStart: undefined,
        qyyxAcEnd: undefined,
        qyyxAcXq1: undefined,
        qyyxAcXq1Name: undefined,
        qyyxAcXq2: undefined,
        qyyxAcXq2Name: undefined,
        qyyxAcPp: undefined,
        qyyxAcPpName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      token:''
    };
  },
  created() {
    // this.getList();
    let token = this.$route.query.token
    if(token){
      this.token = token
      setToken(token)
      this.$store.commit('SET_TOKEN', token)
      setLesseeId('13')
      this.$store.commit('SET_LESSEEID', '13')
      this.getList();
    }
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
  },
  activated(){
    this.getList();
  },
  methods: {
    toEwm(row){
      if(this.token){
        this.$router.push('/qyyx/activityOpen/ewm/'+row.id)
      }else{
        this.$router.push('/sc/activity/ewm/'+row.id)
      }
    },
    /** 查询全员营销活动信息列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否有效字典翻译
    qyyxAcHkTypeFormat(row, column) {
      if(row.qyyxAcHkType=='1'){
        return '积分'
      }else if(row.qyyxAcHkType=='2'){
        return '现金'
      }else{
        return row.qyyxAcHkType
      }
    },
    qyyxAcCjTypeFormat(row, column){
      if(row.qyyxAcCjType=='1'){
        return '积分'
      }else if(row.qyyxAcCjType=='2'){
        return '现金'
      }else{
        return row.qyyxAcCjType
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isActive: undefined,
        lesseeId: undefined,
        qyyxAcName: undefined,
        qyyxAcImg: undefined,
        qyyxAcShareImg: undefined,
        qyyxAcContent: undefined,
        qyyxAcStart: undefined,
        qyyxAcEnd: undefined,
        qyyxAcXq1: undefined,
        qyyxAcXq1Name: undefined,
        qyyxAcXq2: undefined,
        qyyxAcXq2Name: undefined,
        qyyxAcPp: undefined,
        qyyxAcPpName: undefined,
        qyyxAcHk: undefined,
        qyyxAcHkType: undefined,
        qyyxAcCj: undefined,
        qyyxAcCjType: undefined,
        qyyxAcKf: undefined,
        lzjbYl1: undefined,
        lzjbYl2: undefined,
        lzjbYl3: undefined,
        lzjbYl4: undefined,
        lzjbYl5: undefined,
        lzjbYl6: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加全员营销活动信息";
      if(this.token){
        this.$router.push('/qyyx/activityOpen/detail/new')
      }else{
        this.$router.push('/sc/activity/detail/new')
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      const id = row.id || this.ids
      // getActivity(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改全员营销活动信息";
      // });
      if(this.token){
        this.$router.push('/qyyx/activityOpen/detail/'+id)
      }else{
        this.$router.push('/sc/activity/detail/'+id)
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除全员营销活动信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delActivity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有全员营销活动信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportActivity(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
